# -*- coding: utf-8 -*-
#   mesQWidget基本类
# @Time    ： 2023/2/2  21:41
# @Auther  ： 徐良
# File     :  baseQWidget.py
from PySide6.QtWidgets import QWidget
from PySide6.QtCore import Qt, QDate
from share.bioDialog import Infomation
from basePanel.aboutPanel import AboutPanel
import share.globalvar as gol
import os


class BaseQWidget(QWidget):

    def __init__(self, parent=None):
        super(BaseQWidget, self).__init__()

        self.setAttribute(Qt.WA_StyledBackground)
        self.pageFlag = '单页面界面'
        self.beginDate = QDate.currentDate().addDays(-360).toString(Qt.ISODate)
        self.endDate = QDate.currentDate().addDays(60).toString(Qt.ISODate)

    def closeEvent(self, event):
        """
        窗口关闭事件
        :param event:
        :return:
        """
        #
        print('关闭事件发生')
        # userDict = gol.getValue('userDict')
        event.accept()

    def save(self):
        """
        保存单据，即有表头和表体的单据
        :return:
        """
        pass

    def goBack(self):
        """
        单据编辑界面退回到单据浏览界面
        :return:
        """
        ret = self.beforeSave()
        if ret:
            return
        else:
            self.pageGoback()

    def actionDo(self, mWin, q):
        self.gloWin = mWin
        sel = q.text()
        if sel == '关闭':
            if self.beforeClose():
                mWin.closeTab()
        elif sel == '保存':
            self.save()
        elif sel == '退回':
            self.goBack()
        elif sel == '关于':
            self.about = AboutPanel()
            self.about.show()

        elif sel == "帮助":
            Infomation.info('【帮助】功能正在开发')

        else:
            self.actionSelect(q)  # 转到menuActionCommon通用处理类中

    # 模块切换时触发
    def tabChangeBefore(self):
        return True

    def beforeClose(self):
        return True

    def getButton(self, btnName):

        actionList = gol.getValue('actionList')

        ret = None
        for obj in actionList:
            if obj.text() == btnName:
                ret = obj
                break
        return ret

    def checkSave(self):
        btn = self.getButton('保存')
        return btn.isEnabled()

    def removeFile(self, fname):
        if os.path.exists(fname):
            try:
                os.remove(fname)
            except Exception as e:
                print(f"An error occurred while deleting the file: {str(e)}")


    def getButton(self, btnName):
        actionList = gol.getValue('actionList')
        ret = None
        for obj in actionList:
            if obj.text() == btnName:
                ret = obj
                break
        return ret

    def checkSave(self):
        btn = self.getButton('保存')
        return btn.isEnabled()

    def setToolbarSaveButton(self, iAction):
        """
        设置工具栏【保存】命令
        :param iAction:
        :return:
        """
        btn = self.getButton('保存')
        if btn is not None:
            btn.setEnabled(iAction)


